using System;
using System.Collections;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using Sedna.Core.Controller;
using Sedna.Core.Controls.Support;

namespace Sedna.Core.Controls
{
	/// <summary>
	/// Frame, that hosts &lt;IFRAME&gt; tag, to load some document into, and exposes additional
	/// methods on the client.
	/// </summary>
	public class DocumentFrame: Frame
	{
	    //It is needed to avoid ugly "Do you want to dislay non-secure items?" bug in IE
        private const String BLANK_PAGE_URL = "~/Core/pages/blank.htm";
	    
	    
//		private string url;
		private HtmlInputHidden urlHidden=new HtmlInputHidden();

		public string Url
		{
			get { return urlHidden.Value; }
			set { urlHidden.Value = value; }
		}

		protected string WindowName
		{
			get
			{
				return ClientID+"_iframe";
			}
		}
		

        public DocumentFrame():base()
        {
            //TODO: refctor
            JavascriptClass = "DocumentFrame";
            ConfigurationId="configuration.default.DocumentFrame";
            SetUp();
        }

        public DocumentFrame(IConfiguration configuration):base(configuration)
        {
            SetUp();
            Content = new System.Web.UI.LiteralControl();
        }

        private void SetUp()
        {
            Content = new System.Web.UI.LiteralControl();
        }
//	    
//	    public DocumentFrame(IConfiguration configuration):base(configuration)
//	    {
//            Content = new LiteralControl();
//	    }
//

	    protected override void Build()
	    {
	        base.Build();
            Url = ResolveUrl(Configuration.Url);
	    }

	    protected override void CreateChildControls()
		{
			base.CreateChildControls();
			urlHidden.ID="urlhidden";
			Controls.Add(urlHidden);
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
			((System.Web.UI.LiteralControl)Content).Text=GenerateIframeHtml();
//			Page.RegisterHiddenField(UrlHiddenName, Url);
		}

		protected override void Render(HtmlTextWriter writer)
		{
			base.Render(writer);
			urlHidden.RenderControl(writer);
		}

		private string GenerateIframeHtml()
		{
			return String.Format("<iframe name=\"{0}\" id=\"{0}\" height=\"{1}\" width=\"100%\" frameborder=\"0\" scrolling=\"no\" src=\"{2}\"></iframe>", WindowName, Height, ResolveUrl(BLANK_PAGE_URL));
//			return String.Format("<iframe name=\"{0}\" id=\"{0}\" src=\"{1}\" height=\"{2}\" width=\"100%\" frameborder=\"0\" scrolling=\"no\"></iframe>", WindowName ,ResolveUrl(Url), Height);
		}

		protected override IDictionary BuildScriptParameters()
		{
			IDictionary parameters=base.BuildScriptParameters();
			parameters.Add("windowName", WindowName);
			parameters.Add("urlControlId", urlHidden.ClientID);
			return parameters;
		}
	}
}
